<?php
namespace Admin\Controller;
use Think\Controller;
class MerchantController extends IndexController{

    public function lists(){
        $manager=D("Manager");
        if($_SESSION['name']=="admin"){
            $where='id=6';
        }else{
            $where='username="'.$_SESSION['name'].'"';
        }

        $type=I("request.type");
        
        $shopid=$manager->where($where)->getField("shop_id");
        $this->assign('shopid',$shopid);
        if($type==2){
            $this->updateAction();
            header("Location: index.php?m=Admin&c=Merchant&a=lists");
        }
        $this->display();
    }



    public function update_list(){
        $manager=D("Manager");
        if($_SESSION['name']=="admin"){
            $where='id=6';
        }else{
            $where='username="'.$_SESSION['name'].'"';
        }        
        $shopid=$manager->where($where)->getField("shop_id");
        $shopBase= D("Shop_base");
       $shopClass=D("Shop_class");
       $type=I("request.type");
       if($type==1){
       $opt="id=".$shopid;
       $result=$shopBase->where($opt)
                        ->field("lid,name,en_name,phone,cn_abstract,en_abstract,addr,standards,life_description,characteristic,gift,not_contain,logo_img,create_time,update_time")
                        ->find();
           //==============================把时间戳转为时间=========================================
           $result['create_time']=date("Y-m-d/H:i:s",$result['create_time']);
           $result['update_time']=date("Y-m-d/H:i:s",$result['update_time']);
           //=============================替换类别把id替换成名称======================================
           if ($_COOKIE["think_language"] == 'en-us') {
               $result['name']=$result['en_name']!=''?$result['en_name']:$result['name'];
               $result['cn_abstract']=$result['en_abstract']!='' ? $result['en_abstract']:'';
               $libName=$shopClass->where("id=".$result['lid'])->getField("en_name");
               $result['lid']=$libName;
           }

           if ($_COOKIE["think_language"] == 'zh-cn') {
               $libName=$shopClass->where("id=".$result['lid'])->getField("name");
               $result['lid']=$libName;
           }

        if(!$result){
            echo json_encode(0);exit;
        }
           echo json_encode($result);exit;
      }
    }

    public function get_imgs_list(){
        $manager=D("Manager");
        if($_SESSION['name']=="admin"){
            $where='id=6';
        }else{
            $where='username="'.$_SESSION['name'].'"';
        }        
        $shopid=$manager->where($where)->getField("shop_id");
        $shopBase= D("Shop_base");
        $imgs=$shopBase->where("id=".$shopid)->getField("imgs");
        $imgString=rtrim($imgs,',');
        $imgArray=explode(",",$imgString);
        if(!$imgArray){
            echo json_encode(0);
        }else{
            echo json_encode($imgArray);
        }
    }
    //=================================修改图片============================================
    public function update_imgs(){
        $manager=D("Manager");
        if($_SESSION['name']=="admin"){
            $where='id=6';
        }else{
            $where='username="'.$_SESSION['name'].'"';
        }        
        $shopid=$manager->where($where)->getField("shop_id");
        $imgNum = I("request.imgNum");
        $shopBase=D("shop_base");
        $where="id=".$shopid;
        $imgs = $shopBase->where($where)->getField('imgs');
        $res = explode(',', $imgs);
        $repaceString = $res[$imgNum];
        $str = str_replace($repaceString . ',', "", $imgs);
        $data['imgs'] = $str;
        unlink("./imageShop/" . $repaceString);
            $where="id=".$shopid;
            $shopBaselResult = $shopBase->where($where)->save($data);
            if (!$shopBaselResult) {
                echo json_encode(0); exit;
            } else {
                echo json_encode($shopBaselResult); exit;
            }
     }
       public function updateAction(){
          //如何判断商品名不为空
           $shopBase=D("Shop_base");
           $data['sid']=I("post.sid");//要改shop name
           $data['cn_abstract']=I("post.cn_abstract");
           $data['en_abstract']=I("post.en_abstract");
           $data['standards']=I("post.standards");
           $data['life_description']=I("post.life_description");
           $data['characteristic']=I("post.characteristic");
           $data['gift']=I("post.gift");
           $data['not_contain']=I("post.not_contain");
           $data['remark']=I("post.remark");
           $data['update_time']= mktime();
                $manager=D("Manager");
                if($_SESSION['name']=="admin"){
                    $where='id=6';
                }else{
                    $where='username="'.$_SESSION['name'].'"';
                }
                $shopid=$manager->where($where)->getField("shop_id");
           if ($_FILES['imgs']['name'][0] != null) {
               $file = $_FILES;
               $newFile = array();
               foreach ($file as $key => $value) {
                   $num = count($value['name']);
                   for ($i = 0; $i < $num; $i++) {
                       $newFile[$i]['name'] = $value['name'][$i];
                       $newFile[$i]['type'] = $value['type'][$i];
                       $newFile[$i]['tmp_name'] = $value['tmp_name'][$i];
                       $newFile[$i]['error'] = $value['error'][$i];
                       $newFile[$i]['size'] = $value['size'][$i];
                   }
               }
               
               
               
               header("content-type:text/html;charset=utf-8");

               $upload = new \Think\Upload(); // 实例化上传类
               $upload->maxSize = 1024 * 1024 * 10;// 设置附件上传大小 (-1) 是不限值大小
               $upload->exts  = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
               $upload->rootPath = "./imageShop/";
               $upload->replace = true; //存在同名文件是否是覆盖
               // 是否使用子目录保存上传文件
               $upload->autoSub = true;
               $info = $upload->upload($newFile);
               if (!$info) {
                   $data['imgs'] = "";
               } else {
                   //拼接上传的图片路径用于上传到服务器
                   $path = '';
                   for ($i = 0; $i <= count($info) - 1; $i++) {
                       $path .= ltrim($info[$i]['savepath'], './') . $info[$i]['savename'] . ',';
                   }
                        
                       $getData = $shopBase->where("id=".$shopid)->getField('imgs');
                   if ($path != null) {
                       $data['imgs'] = $path . $getData;
                   } else {
                       $data['imgs'] = $getData;
                   }
               }
            }else if ($_FILES['logo_img']['name'] != null) {

                   $r = $shopBase->where('id='.$shopid)->find();
                   if ($r['logo_img'] != null) {
                       unlink("./imageShopLog/" . $r['logo_img']);
                   }
               header("content-type:text/html;charset=utf-8");
               $upload = new \Think\Upload(); // 实例化上传类
               $upload->maxSize = 1024 * 1024;// 设置附件上传大小 (-1) 是不限值大小
               $upload->exts  = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
               $upload->rootPath ="./imageShopLog/";
               // $upload->savePath = "/Hos";// 设置附件上传目录
               $upload->replace = true; //存在同名文件是否是覆盖
               // 是否使用子目录保存上传文件
               $upload->autoSub = true;
               // 采用date函数生成命名规则 传入Y-m-d参数
               $upload->saveName = time() . '_' . mt_rand();
               $info = $upload->uploadOne($_FILES['logo_img']);
               if (!$info) {
                   $data['logo_img'] = '';
               } else {
                   $data['logo_img'] = ltrim($info['savepath'] . $info['savename'], '/');
               }
           }
           //修改ID
           $shopBase->where("id=".$shopid)->save($data);
     }

   }
